﻿
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoSingleton<UIManager>
{
    public Transform panel_parent;
    public RawImage video_plane, video_plane_over;
    public Slider progressSlider;
    public RectTransform leftiden, rightiden;
    public GameObject settingPanel;
    private Dropdown window_size_dropdown;
    private InputField fastfaward, in_single_frame, jump_forward;
    private Toggle toggle_zhuanma;
    private int sliderCount;

    protected override void Init()
    {
        base.Init();

        window_size_dropdown = settingPanel.transform.Find("window_size").GetComponentInChildren<Dropdown>();
        fastfaward = settingPanel.transform.Find("fast_forward").GetComponentInChildren<InputField>();
        in_single_frame = settingPanel.transform.Find("in_single_frame").GetComponentInChildren<InputField>();
        jump_forward = settingPanel.transform.Find("jump_forward").GetComponentInChildren<InputField>();
        toggle_zhuanma = settingPanel.transform.Find("toggle_cut_select").GetComponentInChildren<Toggle>();

        progressSlider.onValueChanged.AddListener((value) => {
            if (!AppManager.Instance.IsPlaying)
            {
                //位置原因，当这里队Progress设置频率过高时，会使单帧Seek等功能异常
                //所以这里做一定延时减少设置的频率
                sliderCount++;
                if (sliderCount > 30)
                {
                    AppManager.Instance.Progress = value;
                    sliderCount = 0;
                }
            }
        });

        window_size_dropdown.onValueChanged.AddListener((value) => {
            switch (value)
            {
                case 0:Screen.SetResolution(1280, 720,false); break;
                case 1: Screen.SetResolution(1600, 900, false); break;
                case 2: Screen.SetResolution(1920, 1080, false); break;
                default:
                    break;
            }
            AppManager.Instance.appconfig.screen_resolution_dropdownindex = value;
            AppManager.Instance.AppConfigSave();
        });

        fastfaward.onEndEdit.AddListener((v) => {
            AppManager.Instance.appconfig.fastfaward_frames_perOpt = float.Parse(v);
            AppManager.Instance.AppConfigSave();
        });

        in_single_frame.onEndEdit.AddListener((v) => {
            AppManager.Instance.appconfig.in_single_frames_perOpt = float.Parse(v);
            AppManager.Instance.AppConfigSave();
        });

        jump_forward.onEndEdit.AddListener((v) => {
            AppManager.Instance.appconfig.jump_forward_frame_perOpt = float.Parse(v);
            AppManager.Instance.AppConfigSave();
        });

        toggle_zhuanma.onValueChanged.AddListener((v) => {
            AppManager.Instance.appconfig.isZhuanma = v;
            AppManager.Instance.AppConfigSave();
        });

    }

    public void SetVideoPanelScaleFromTex(Texture tex)
    {
        float screenAspectWH = 1.7778f;
        float texAspectHW = tex.height / (float)tex.width;
        panel_parent.transform.localScale = new Vector3(1/(screenAspectWH * texAspectHW), 1,1);
    }

    public Texture VideoPlaneTexture
    {
        set
        {
            video_plane.texture = value;
        }
    }

    public Texture VideoPlaneOverTexture
    {
        set
        {
            video_plane_over.texture = value;
        }
    }

    public float IdentifyLeft
    {
        set
        {
            RectTransform rectTrans = progressSlider.GetComponent<RectTransform>();
            leftiden.anchoredPosition = new Vector2(value * rectTrans.rect.width, leftiden.anchoredPosition.y);
        }
    }

    public float IdentifyRight
    {
        set
        {
            RectTransform rectTrans = progressSlider.GetComponent<RectTransform>();
            rightiden.anchoredPosition = new Vector2(value * rectTrans.rect.width, rightiden.anchoredPosition.y);
        }
    }

    public float SliderValue
    {
        set { progressSlider.value = value; }
    }

    private void Start()
    {
        AppConfig appconfig = AppManager.Instance.appconfig;
        window_size_dropdown.value = appconfig.screen_resolution_dropdownindex;
        fastfaward.text = appconfig.fastfaward_frames_perOpt.ToString();
        in_single_frame.text = appconfig.in_single_frames_perOpt.ToString();
        jump_forward.text = appconfig.jump_forward_frame_perOpt.ToString();
        toggle_zhuanma.isOn = appconfig.isZhuanma;
    }

    private void Update()
    {
        if (AppManager.Instance.IsPlaying)
        {
            progressSlider.value = AppManager.Instance.Progress;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            Color scol = video_plane_over.color;
            scol.a += 0.1f;
            scol.a = Mathf.Clamp01(scol.a);
            video_plane_over.color = scol;
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            Color scol = video_plane_over.color;
            scol.a -= 0.1f;
            scol.a = Mathf.Clamp01(scol.a);
            video_plane_over.color = scol;
        }

        if (Input.GetKeyDown(KeyCode.H))
        {
            settingPanel.SetActive(!settingPanel.activeInHierarchy);
        }

    }

    public void OnCapture(Texture texture)
    {
        VideoPlaneOverTexture = texture;
    }

    public void ShowHideVideoPlaneOver()
    {
        bool activeInHierarchy = video_plane_over.gameObject.activeInHierarchy;
        video_plane_over.gameObject.SetActive(!activeInHierarchy);
        if (!activeInHierarchy)
        {
            Texture capture = AVProVideoPlayerMgr.Instance.CapturePicture();
            OnCapture(capture);
        }
    }

}
